﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Service.Impl;
using System.Collections;

namespace DAL.Service.admin.Impl
{
    public class OperatorServiceImpl : BaseServiceImpl, IOperatorService
    {
        public IList getOperator(string workid, string pwd)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select * from cms.Operator where O_Workid=@workid and O_Password=@pwd");
            query.addParas("@workid", workid);
            query.addParas("@pwd", pwd);
            return  base.getList(query);
        }

        public int delete(string ids)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("DELETE FROM cms.[Operator] WHERE o_id in(" + ids + ")"); ;
            return base.exeUpdateSql(query);
        }

        public IList getOperatorbyid(int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("SELECT * FROM cms.Operator WHERE O_id=@oid");
            query.addParas("@oid", id);
            return base.getList(query);
        }

        public IList getOperator(int pageno, int pagesize)
        {
            MyQuery query = new MyQuery();
            StringBuilder sb = new StringBuilder();
            if (pageno == 1)
            {
                sb.Append("SELECT count(*) as total FROM cms.Operator where  O_workid != 'admin' and  O_DeleteFlag=@flag;");
                sb.Append("select top " + pagesize + " * from cms.Operator where O_workid != 'admin' and O_DeleteFlag=@flag order by o_id desc;");
            }
            else
            {
                sb.Append("select * from(");
                sb.Append("select *,ROW_NUMBER() OVER (order by o_id desc) as rno from  cms.Operator where O_DeleteFlag=@flag ");
                sb.Append(") as a where  O_workid != 'admin' and  a.rno>@start and a.rno<@end  order by o_id desc;");
                query.PageNo = pageno;
                query.PageSize = pagesize; ;
            }
            query.setQueryString(sb.ToString());
            query.addParas("@flag", (int)DAL.Enums.operatorDelFlag.正常);
            return base.getList(query);
        }

        public int updatepwd(string pwd, int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("UPDATE cms.Operator SET O_Password =@pwd WHERE o_id =@oid");
            query.addParas("@pwd", pwd);
            query.addParas("@oid", id);
            return this.exeUpdateSql(query);
        }

        public int getCountByWorkid(string workid,int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString(" select count(1) as total from cms.Operator where O_Workid='" + workid + "'" + " and O_id !=" + id);
            //query.setQueryString(" select count(1) as total from cms.Role where R_Name='" + name + "'" + " and R_id != " + id);
            Hashtable ht = (Hashtable)this.getList(query)[0];
            return Convert.ToInt32(ht["total"]);
        }

        public int getCountByRid(int rid)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select count(O_id) as total from cms.Operator where O_Rid=@rid");
            query.addParas("@rid", rid);
            Hashtable ht = (Hashtable)this.getList(query)[0];
            return Convert.ToInt32(ht["total"]);
        }
    }
}
